/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.h
 * @brief          : Header for main.c file.
 *                   This file contains the common defines of the application.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"  // IWYU pragma: export

    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
#include "pid.h"
    /* USER CODE END Includes */

    /* Exported types ------------------------------------------------------------*/
    /* USER CODE BEGIN ET */

    typedef enum
    {
        kOFF,
        kON,
    } switch_state_t;

    typedef enum
    {
        kOK,
        kERR,
        kRESTART,
    } error_state_t;

    typedef enum
    {
        kModule1st,
        kModule2nd,
        kModule3rd,

        kModuleNum
    } pa_module_id_t;

    typedef enum
    {
        kSubCtrl1st,
        kSubCtrl2nd,
        kSubCtrl3rd,
        kSubCtrl4th,
        kSubCtrl5th,
        kSubCtrl6th,
        kSubCtrl7th,
        kSubCtrl8th,
        kSubCtrl9th,

        kSubCtrlNum
    } sub_control_id_t;

    typedef struct
    {
        uint8_t current;
        uint8_t rf_wave;
    } adc_pin_index_t;

    typedef struct
    {
        GPIO_TypeDef *port;
        uint16_t pin;
        float *val_ptr;
    } ds18b20_config_t;

    typedef struct
    {
        GPIO_TypeDef *port;
        uint16_t pin;
    } pd_pin_index_t;

    typedef struct
    {
        const adc_pin_index_t *adc_index;
        const ds18b20_config_t *temp_index;
        const pd_pin_index_t *pd_index;
    } pa_module_hardware_config_t;

    typedef struct
    {
        float current;
        float rf_wave;
        float temperature;
        const pa_module_hardware_config_t *hardware;
    } pa_module_t;

    typedef struct
    {
        uint8_t pa_enable : 3;
        switch_state_t auto_run : 1;
        error_state_t error : 4;
        uint8_t soft_reset_count : 8;
    } sub_control_state_t;

    typedef struct
    {
        float att_val;   // 衰减器值
        float phase_val; // 相位值
        pid_config_t forward_pid;
        pid_config_t reverse_pid;
    } sub_config_t;
    /**
     * @brief 分控制器flash数据结构体
     *
     */
    typedef struct
    {
        uint8_t init;
        uint8_t ip[4];
        sub_config_t config;
        /*待定*/
    } __ALIGNED(4) sub_control_flash_t;
    /**
     * @brief 分控制器结构体
     *
     */
    typedef struct
    {
        uint8_t frame_header;      // 帧头
        uint8_t init;              // 初始化标志
        sub_control_state_t state; // 状态
        float current;             // 电流
        pa_module_t pa[kModuleNum]; // PA模块
        struct
        {
            float target;
            float forward;
            float reverse;
        } rf_power;                // 射频功率
        sub_control_flash_t flash; // flash数据
        union {
            uint8_t byte[4];
            uint16_t half[2];
            uint32_t word;
        } check;
    } sub_control_t;

    /* USER CODE END ET */

    /* Exported constants --------------------------------------------------------*/
    /* USER CODE BEGIN EC */

    /* USER CODE END EC */

    /* Exported macro ------------------------------------------------------------*/
    /* USER CODE BEGIN EM */

    /* USER CODE END EM */

    /* Exported functions prototypes ---------------------------------------------*/
    void Error_Handler(void);

    /* USER CODE BEGIN EFP */
    void vApplicationIdleHook();
    void Flash_Init(sub_control_flash_t *flash_p);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define LED_Pin GPIO_PIN_15
#define LED_GPIO_Port GPIOC
#define JB_IN3_Pin GPIO_PIN_0
#define JB_IN3_GPIO_Port GPIOA
#define DL_JC3_Pin GPIO_PIN_1
#define DL_JC3_GPIO_Port GPIOA
#define JB_IN4_Pin GPIO_PIN_2
#define JB_IN4_GPIO_Port GPIOA
#define DL_JC4_Pin GPIO_PIN_3
#define DL_JC4_GPIO_Port GPIOA
#define JB_IN2_Pin GPIO_PIN_4
#define JB_IN2_GPIO_Port GPIOA
#define DL_JC2_Pin GPIO_PIN_5
#define DL_JC2_GPIO_Port GPIOA
#define JB_IN1_Pin GPIO_PIN_6
#define JB_IN1_GPIO_Port GPIOA
#define M_DQ4_Pin GPIO_PIN_7
#define M_DQ4_GPIO_Port GPIOA
#define M_DQ3_Pin GPIO_PIN_0
#define M_DQ3_GPIO_Port GPIOB
#define M_PTT3_Pin GPIO_PIN_1
#define M_PTT3_GPIO_Port GPIOB
#define M_PTT4_Pin GPIO_PIN_2
#define M_PTT4_GPIO_Port GPIOB
#define M_DQ2_Pin GPIO_PIN_8
#define M_DQ2_GPIO_Port GPIOA
#define M_PTT2_Pin GPIO_PIN_12
#define M_PTT2_GPIO_Port GPIOA
#define PE_LE_Pin GPIO_PIN_15
#define PE_LE_GPIO_Port GPIOA
#define PE_SCK_Pin GPIO_PIN_3
#define PE_SCK_GPIO_Port GPIOB
#define M_PTT1_Pin GPIO_PIN_4 // 额外的PD
#define M_PTT1_GPIO_Port GPIOB //
#define PE_SDA_Pin GPIO_PIN_5
#define PE_SDA_GPIO_Port GPIOB
#define ATT_PWM_Pin GPIO_PIN_6
#define ATT_PWM_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */
#define ENABLE_DEBUG_MODE 0
#define DEFAULT_INITIAL_VALUE 0x01

#define UART_SERIAL USART3
#define UART_RS485 USART1

#define SAMPLING_RESISTOR 0.0025f
#define MAX4080_GAIN 60

#define ADC_CHANNEL_NUM 7 // ADC采样次数
#define ADC_SUM_NUM 10 // ADC采样次数
//#define JB_IN1_ADC_CHANNEL NULL
#define JB_IN2_ADC_CHANNEL 3
#define JB_IN3_ADC_CHANNEL 0
#define JB_IN4_ADC_CHANNEL 2
#define DL_JC1_ADC_CHANNEL 5
#define DL_JC2_ADC_CHANNEL 4
#define DL_JC3_ADC_CHANNEL 1
#define DL_JC4_ADC_CHANNEL 6

//#define DS18B20_PIN_NUM_1 NULL
#define DS18B20_PIN_NUM_2 8
#define DS18B20_PIN_NUM_3 0
#define DS18B20_PIN_NUM_4 7

#define LED_ON() HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET)
#define LED_OFF() HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET)

#define PA_MODULE_ENABLE_BIT 0x07
#define PA_MODULE_DISABLE_BIT 0x00

#define SERIAL_FRAME_HEADER 0x35

#define RS485_FRAME_HEADER 0x34
#define SUBCTRL_BASE_ADDRESS 101
#define RS485_UNIVERSAL_ADDRESS 233
#define RS485_TYPE_QUERY 0x00
#define RS485_TYPE_SAVE_PARAMETER 0x01
#define RS485_TYPE_POWER 0x02
#define RS485_TYPE_PHASE 0x03
#define RS485_TYPE_ATTENUATION 0x04
#define RS485_TYPE_AUTO_RUN 0x05
#define RS485_TYPE_FORWAR_PID 0x06
#define RS485_TYPE_REVERSE_PID 0x07
    /* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
